package com.jdkj.tools.util;

import com.ruiyun.jvppeteer.api.core.Browser;
import com.ruiyun.jvppeteer.api.core.Page;
import com.ruiyun.jvppeteer.cdp.core.Puppeteer;
import com.ruiyun.jvppeteer.cdp.entities.Cookie;
import com.ruiyun.jvppeteer.cdp.entities.LaunchOptions;
import lombok.SneakyThrows;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.sql.SQLOutput;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class LoginUtil {

    @SneakyThrows
    @Async
    public void getCookie(String username, String password) {
        String loginUrl="http://192.168.11.7:8083/login.jsp";
        Map<String,Object> map=new HashMap<>();
        map.put("os_username",username);
        map.put("os_password",password);
        map.put("os_destination","");
        map.put("user_role","");
        map.put("atl_token","");
        map.put("login","登录");
        String str = HttpClientUtil.getLoginCookie(loginUrl, map, new Header[]{
                new BasicHeader("Content-Type", "application/x-www-form-urlencoded"),
        new BasicHeader("Cookie", "JSESSIONID=B936F3FFD1082E1DEC1F3A37E3309309; atlassian.xsrf.token=BLUR-YY6T-UN94-9T15_9237234f3cdb98baac26d33de9546ff914393f6e_lout")});


        TokenConstant.JSESSIONID=str.substring(0,str.indexOf(";")+1)+"atlassian.xsrf.token=BLUR-YY6T-UN94-9T15_9237234f3cdb98baac26d33de9546ff914393f6e_lout";
        System.out.println("token结果"+TokenConstant.JSESSIONID);

        String getURL= "http://192.168.11.7:8083/";
        String cookie = HttpClientUtil.getLoginCookie(getURL, new HashMap<>(), new Header[]{
                new BasicHeader("Cookie", TokenConstant.JSESSIONID),
        });
        TokenConstant.JSESSIONID=cookie.substring(0,cookie.indexOf(";")+1)+str.substring(0,str.indexOf(";"));
        System.out.println("最后的token  "+TokenConstant.JSESSIONID);
        //        String chrome = "/usr/bin/chromium-browser";
//        LaunchOptions launchOptions = LaunchOptions.builder()
//                .headless(true)  // 启动有头浏览器
//                .timeout(3000)   // 设置超时时间
//                .executablePath(chrome)  // 设置 Chrome 的路径
//                .build();  //这个LaunchOptions创建使用了建造者模式
//        Browser browser = Puppeteer.launch(launchOptions); //启动浏览器
//        Page page = browser.newPage();

//        page.goTo("http://192.168.11.7:8083/login.jsp");
//        //
//        page.type("#login-form-username", username);
//        page.type("#login-form-password", password);
//
//        page.$("#login-form-submit").click();
//
//        page.waitForFrame("http://192.168.11.7:8083/secure/RapidBoard.jspa?rapidView=79");

//        List<Cookie> cookies = page.cookies();
//        StringBuffer resultCookie =new StringBuffer();
//       for(Cookie cookie : cookies){
//           resultCookie.append(cookie.getName()).append("=").append(cookie.getValue()).append(";");
//       }
//        TokenConstant.JSESSIONID=resultCookie.toString();
//        System.out.println("拿到token"+TokenConstant.JSESSIONID);
    }

    public static void main(String[] args) {
        LoginUtil loginUtil=new LoginUtil();
        loginUtil.getCookie("zhouxin","123456");
    }
}
